<?php
class Administrator extends Controller{
	function __construct(){
        parent::Controller();
	    $this->is_logged_in();
    }
	
	function index(){
		if($this->session->userdata('access_lev') == 1){
			$data["title"] = "Administrator.";	//set title.
			$data["topic"] = "Administrator - Knowledge Management.";
	
			//load view
			$this->load->view('inc/administrator/main_content',$data);
		}
		else{
				$data["title"] = "Permission Deny.";
                $data["topic"] = "You don have permission to access this page.";
				$data["content"]='msg/permission_deny';
				$views = $this->load->view('inc/one_cols',$data);	//load view one cols.	
		}
	}
	
	function is_logged_in(){
		$is_logged_in = $this->session->userdata('is_logged_in');
	    if(!isset($is_logged_in) || $is_logged_in != true)
	    {
                /*$data["title"] = "Permission Deny.";
                $data["topic"] = "You don have permission to access this page.";
				$data["content"]='msg/permission_deny';
				$views = $this->load->view('inc/one_cols',$data);	//load view one cols.*/	
				$path = base_url();
				redirect($path);				
	    }
	}

	function org_manage_staff(){	//load view staff
		$data["title"] = "Administrator - Manage Staff";
		$data["topic"] = "Administrator - Manage Staff";
		$this->load->view('inc/administrator/org_manage_staff',$data);
	}
	
	function choose_staff($id){	//load view choose staff
		$data["title"] = "Administrator - Manage Staff";
		$data["topic"] = "Choose staff of organization - ";
		$data["id"] = $id;
		$this->load->view('inc/administrator/org_choose_staff',$data);
	}
	
	function staff_profiles(){
		$member_id = $this->input->post('member_id');
		$data["member_id"] = $member_id;
		$this->load->view('inc/organization/staff_profiles_ajax',$data);
	}
	
	function assign_staff_db(){	//query choose staff ajax
		$staff_id = $this->input->post('staff_id');
		$org_id = $this->input->post('org_id');	
		$data["staff_id"] = $staff_id;
		$data["org_id"] = $org_id;
		
		$this->load->model('organization_model');
		$query = $this->organization_model->assign_staff($data);
		if($query){
			$this->load->view('msg/update_staff_success');
		}
		else
		{
			$this->load->view('msg/update_staff_fail');
		}
	}
	
	function manage_organization(){
		$data["title"] = "Administrator - Manage Organization";
		$data["topic"] = "Manage Organization";
		$this->load->view('inc/administrator/manage_org',$data);
	}
	
	function edit_organization(){
		$this->load->model('organization_model');
		$query = $this->organization_model->edit_organization();
		
		if($query){
			$path = base_url()."administrator/org/manage-org";
			redirect($path);
		}
		else
		{
			echo "false";
		}
	}
	
	function add_organization(){
		$this->load->model('organization_model');
		$query = $this->organization_model->add_organization();
		
		if($query){
			echo "ok";
		}
		else
		{
			echo "false";		
		}
	}
	
	function blog_category_control(){
		$data["title"] = "Administrator - Blog Category";
		$data["topic"] = "Manage Blog Category";
		$this->load->view('inc/administrator/blog_category_control',$data);
	}
	
	function add_blog_category(){
		if(isset($_POST['add_category'])) {
						  
			  //$path = base_url().'km_resource/member_display';
			  $config['upload_path'] = './km_resource/blog_category/';
			  $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
			  $config['max_size']  = '0';
			  $config['max_width']  = '0';
			  $config['max_height']  = '0';
			  /* Load the upload library */
			  $this->load->library('upload', $config);
		
			  /* Create the config for image library */
			  /* (pretty self-explanatory) */
			  $configThumb = array();
			  $configThumb['image_library'] = 'gd2';
			  $configThumb['source_image'] = '';
			  $configThumb['create_thumb'] = TRUE;
			  $configThumb['maintain_ratio'] = TRUE;
			  
			  $configThumb['width'] = 67;
			  $configThumb['height'] = 63;
			  
			  /* Load the image library */
			  $this->load->library('image_lib');
		
			  for($i = 1; $i < 2; $i++) {
				$upload = $this->upload->do_upload('image'.$i);
				if($upload === FALSE) continue;
				$data = $this->upload->data();
				$uploadedFiles[$i] = $data;
				
				if($data['is_image'] == 1) {
				  $configThumb['source_image'] = $data['full_path'];
				  $this->image_lib->initialize($configThumb);
				  $this->image_lib->resize(); 
				}
			  }
				
				//send to model.
				$data['file_name'];
				$data['file_ext'];
				$data['raw_name'];
				$this->load->model('blog_model');
				$query = $this->blog_model->add_blog_category($data);
				
				$path = base_url()."administrator/blog/category-control";
				
				if($query){
					redirect($path);
				}
			}					
	}
	
	function edit_blog_category(){
		if(isset($_POST['category_name'])) {
						  
			  //$path = base_url().'km_resource/member_display';
			  $config['upload_path'] = './km_resource/blog_category/';
			  $config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
			  $config['max_size']  = '0';
			  $config['max_width']  = '0';
			  $config['max_height']  = '0';
			  /* Load the upload library */
			  $this->load->library('upload', $config);
		
			  /* Create the config for image library */
			  /* (pretty self-explanatory) */
			  $configThumb = array();
			  $configThumb['image_library'] = 'gd2';
			  $configThumb['source_image'] = '';
			  $configThumb['create_thumb'] = TRUE;
			  $configThumb['maintain_ratio'] = TRUE;
			  
			  $configThumb['width'] = 67;
			  $configThumb['height'] = 63;
			  
			  /* Load the image library */
			  $this->load->library('image_lib');
		
			  for($i = 1; $i < 2; $i++) {
				$upload = $this->upload->do_upload('image'.$i);
				if($upload === FALSE) continue;
				$data = $this->upload->data();
				$uploadedFiles[$i] = $data;
				
				if($data['is_image'] == 1) {
				  $configThumb['source_image'] = $data['full_path'];
				  $this->image_lib->initialize($configThumb);
				  $this->image_lib->resize(); 
				}
			  }
				
				//send to model.
				$data['file_name'];
				$data['file_ext'];
				$data['raw_name'];
				$this->load->model('blog_model');
				$query = $this->blog_model->edit_blog_category($data);
				
				$path = base_url()."administrator";
				
				if($query){
					redirect($path);
				}
			}					
	}
}